﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace KPIS.GERP.GIMS.APP.APP_CODE
{
    public class FormatMoney
    {
        public string MoneyTextThai(string strNumberText, string strMainCurrency, string strSubCurrency)
        {
            string strResult = "";
            string[] astrPositionName = { "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน" };
            string[] astrDigitName = { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า" };
            string strReverseBaht;
            string strReverseSatang;
            int nPointPosition;
            int nMaxPosition;
            int nCurrentPosition;

            while (strNumberText.Length > 1
                && StringLeft(strNumberText, 1) == "0"
                && StringLeft(strNumberText, 2) != "0.")
                strNumberText = strNumberText.Substring(1);

            // simplify the number
            strNumberText = strNumberText.Trim().Replace(",", "");

            // separate baht and satang
            nPointPosition = strNumberText.IndexOf(".");

            if (nPointPosition >= 0)
            {
                strReverseBaht = StringReverse(strNumberText.Substring(0, nPointPosition));
                strReverseSatang = StringReverse(StringRight(double.Parse(strNumberText.Substring(nPointPosition, 3)).ToString(".00"), 2));
            }
            else
            {
                strReverseBaht = StringReverse(strNumberText);
                strReverseSatang = "00";
            }

            // begin process baht
            nMaxPosition = strReverseBaht.Length - 1;

            for (nCurrentPosition = nMaxPosition; nCurrentPosition >= 0; nCurrentPosition--)
            {
                strResult += EachNumberPositionThaiText(strReverseBaht, nMaxPosition, nCurrentPosition, astrPositionName, astrDigitName);
            }

            if (strResult.Length > 0)
                strResult += strMainCurrency;
            // end process baht

            // begin process satang
            if (strResult.Length > 0 && strReverseSatang == "00")
            {
                strResult += "ถ้วน";
            }
            else
            {
                if (strResult == "ศูนย์ " + strMainCurrency)
                    strResult = "";

                nMaxPosition = strReverseSatang.Length - 1;

                for (nCurrentPosition = nMaxPosition; nCurrentPosition >= 0; nCurrentPosition--)
                    strResult += EachNumberPositionThaiText(strReverseSatang, nMaxPosition, nCurrentPosition, astrPositionName, astrDigitName);

                if (strResult.Length > 0)
                    strResult += strSubCurrency;
            }
            // end process satang

            return strResult;
        }

        public string EachNumberPositionThaiText(string strReverseNumberText, int nMaxPosition
            , int nCurrentPosition, string[] astrPositionName, string[] astrDigitName)
        {
            int nDigit;
            string strDigitName;
            int nPosition;
            string strPositionName;
            int nPreviousDigit = 0;

            nDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition, 1));

            if (nCurrentPosition < nMaxPosition)
                nPreviousDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition + 1, 1));
            // notice: lngPosition = (lngCurrentPosition - 1) Mod 6 + 1
            nPosition = (nCurrentPosition - 1) % 6 + 1;

            if (nDigit == 0 && nMaxPosition > 0 || nDigit == 1 && nPosition == 1)
                strDigitName = "";
            else if (nDigit == 1 && (nPosition == 0 || nPosition == 6)
                && nMaxPosition > nCurrentPosition
                && nPreviousDigit > 0)
                strDigitName = "เอ็ด";
            else if (nDigit == 2 && nPosition == 1)
                strDigitName = "ยี่";
            else
                strDigitName = astrDigitName[nDigit];

            if (nDigit == 0 && nPosition != 6)
                strPositionName = "";
            else
                strPositionName = astrPositionName[nPosition];

            return strDigitName + strPositionName;
        }

        public string StringReverse(string strText)
        {
            int intIndex;
            int intLen = strText.Length;
            string strResult = "";

            for (intIndex = 0; intIndex < intLen; intIndex++)
            {
                strResult += strText[intLen - intIndex - 1];
            }

            return strResult;
        }

        public string StringRight(string strText, int nLength)
        {
            string strResult = "";

            nLength = ZeroUp(nLength);

            if (nLength > strText.Length)
                strResult = strText;
            else if (strText.Length > 0)
                strResult = strText.Substring(strText.Length - nLength, nLength);

            return strResult;
        }

        public string StringLeft(string strText, int nLength)
        {
            string strResult = "";

            nLength = ZeroUp(nLength);

            if (nLength > strText.Length)
                strResult = strText;
            else if (strText.Length > 0)
                strResult = strText.Substring(0, nLength);

            return strResult;
        }

        public int ZeroUp(int nValue)
        {
            int nResult;

            if (nValue >= 0)
                nResult = nValue;
            else
                nResult = 0;

            return nResult;
        }
    }
}
